﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

namespace SAD
{
    class PanelEmergencia
    {
        //Atributos que se van a usar para llenar el panel correspondiente a la opcion "Buscar"
        private System.Windows.Forms.DataGridView dvgEmergencias;
        private TextBox txtBoxBuscar;
        DataSet dtData = new DataSet();
        private Label label1Buscar, label2Buscar;
        private ComboBox combo1Buscar;

        //Atributos que se van a usar para llenar el panel correspondiente a la opcion "NUEVO"
         System.Windows.Forms.GroupBox groupBox1;
         System.Windows.Forms.TextBox textBox1;
         System.Windows.Forms.Label label2;
         System.Windows.Forms.Label label1;
         System.Windows.Forms.GroupBox groupBox2;
         System.Windows.Forms.ComboBox comboTipoEmerg;
         System.Windows.Forms.Label label4;
         System.Windows.Forms.Label label3;
         System.Windows.Forms.DateTimePicker dateTimePicker1;
         System.Windows.Forms.Label label5, label10;
         System.Windows.Forms.ComboBox comboPriorEmerg;
         System.Windows.Forms.Button buttonRegistrar;
         System.Windows.Forms.TextBox textBox2;
         System.Windows.Forms.Button button2;

        public void llenarPanelNuevo(Panel pan)
        {
            List<TipoEmergencia> listaTipoEmerg = null;
            
            
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(VentanaEmergencia));
            groupBox1 = new System.Windows.Forms.GroupBox();
            button2 = new System.Windows.Forms.Button();
            textBox2 = new System.Windows.Forms.TextBox();
            textBox1 = new System.Windows.Forms.TextBox();
            label2 = new System.Windows.Forms.Label();
            label1 = new System.Windows.Forms.Label();
            groupBox2 = new System.Windows.Forms.GroupBox();
            dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            label5 = new System.Windows.Forms.Label();
            comboPriorEmerg = new System.Windows.Forms.ComboBox();
            comboTipoEmerg = new System.Windows.Forms.ComboBox();
            label4 = new System.Windows.Forms.Label();
            label3 = new System.Windows.Forms.Label();
            buttonRegistrar = new System.Windows.Forms.Button();
            label10 = new Label();
            groupBox1.SuspendLayout();
            groupBox2.SuspendLayout();
            // 
            // groupBox1
            // 
            //groupBox1.BackColor = System.Drawing.Color.Transparent;

            groupBox1.Controls.Add(textBox2);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(label2);
            groupBox1.Controls.Add(label1);
            groupBox1.Controls.Add(button2);
            groupBox1.Location = new System.Drawing.Point(12, 50);
            groupBox1.Name = "groupBox1";
            groupBox1.Size = new System.Drawing.Size(343, 90);
            groupBox1.TabIndex = 0;
            groupBox1.TabStop = false;
            groupBox1.Text = "Datos Generales";
            // 
            // button2
            // 
            button2.Location = new System.Drawing.Point(250, 53);
            button2.Name = "button2";
            button2.Size = new System.Drawing.Size(80, 23);
            button2.TabIndex = 4;
            button2.Text = "Buscar";
            button2.UseVisualStyleBackColor = true;
            button2.Click += new System.EventHandler(this.button2_Click);

            // 
            // textBox2
            // 
            textBox2.Location = new System.Drawing.Point(78, 53);
            textBox2.Name = "textBox2";
            textBox2.ReadOnly = true;
            textBox2.Size = new System.Drawing.Size(160, 20);
            textBox2.TabIndex = 3;
            // 
            // textBox1
            // 
            textBox1.Location = new System.Drawing.Point(78, 22);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(250, 20);
            textBox1.TabIndex = 2;
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.Location = new System.Drawing.Point(18, 56);
            label2.Name = "label2";
            label2.Size = new System.Drawing.Size(34, 13);
            label2.TabIndex = 1;
            label2.Text = "Lugar";
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new System.Drawing.Point(18, 25);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(44, 13);
            label1.TabIndex = 0;
            label1.Text = "Nombre";
            // 
            // groupBox2
            // 
            groupBox2.BackColor = System.Drawing.Color.Transparent;
            groupBox2.Controls.Add(dateTimePicker1);
            groupBox2.Controls.Add(label5);
            groupBox2.Controls.Add(comboPriorEmerg);
            groupBox2.Controls.Add(comboTipoEmerg);
            groupBox2.Controls.Add(label4);
            groupBox2.Controls.Add(label3);
            groupBox2.Location = new System.Drawing.Point(12, 162);
            groupBox2.Name = "groupBox2";
            groupBox2.Size = new System.Drawing.Size(343, 94);
            groupBox2.TabIndex = 1;
            groupBox2.TabStop = false;
            groupBox2.Text = "Caracteristicas";
            // 
            // dateTimePicker1
            // 
            dateTimePicker1.Location = new System.Drawing.Point(59, 61);
            dateTimePicker1.Name = "dateTimePicker1";
            dateTimePicker1.Size = new System.Drawing.Size(201, 20);
            dateTimePicker1.TabIndex = 5;
            // 
            // label5
            // 
            label5.AutoSize = true;
            label5.Location = new System.Drawing.Point(6, 67);
            label5.Name = "label5";
            label5.Size = new System.Drawing.Size(37, 13);
            label5.TabIndex = 4;
            label5.Text = "Fecha";
            // 
            // comboPriorEmerg
            // 
            comboPriorEmerg.FormattingEnabled = true;
            comboPriorEmerg.Location = new System.Drawing.Point(268, 23);
            comboPriorEmerg.Name = "comboPriorEmerg";
            comboPriorEmerg.Size = new System.Drawing.Size(60, 21);
            comboPriorEmerg.TabIndex = 3;
            comboPriorEmerg.DropDownStyle = ComboBoxStyle.DropDownList;

            //PRIORIDAD HARDCODEADO
            comboPriorEmerg.Items.Add("1");
            comboPriorEmerg.Items.Add("2");
            comboPriorEmerg.Items.Add("3");
            comboPriorEmerg.Items.Add("4");
            comboPriorEmerg.Items.Add("5");

            // 
            // comboTipoEmerg
            // 
            comboTipoEmerg.FormattingEnabled = true;
            comboTipoEmerg.Location = new System.Drawing.Point(59, 23);
            comboTipoEmerg.Name = "comboTipoEmerg";
            comboTipoEmerg.Size = new System.Drawing.Size(134, 21);
            comboTipoEmerg.TabIndex = 2;
            comboTipoEmerg.DropDownStyle = ComboBoxStyle.DropDownList;

            listaTipoEmerg = GestorEmergencia.ObtenerTiposEmergencias();

            for (int i = 0; i < listaTipoEmerg.Count; i++)
            {
                //ComboMonedaCamp.Items.Add(listaMonedas[i].Substring(13));
                comboTipoEmerg.Items.Add(listaTipoEmerg[i].Nombre);
            }


            // 
            // label4
            // 
            label4.AutoSize = true;
            label4.Location = new System.Drawing.Point(213, 26);
            label4.Name = "label4";
            label4.Size = new System.Drawing.Size(48, 13);
            label4.TabIndex = 1;
            label4.Text = "Prioridad";
            // 
            // label3
            // 
            label3.AutoSize = true;
            label3.Location = new System.Drawing.Point(6, 26);
            label3.Name = "label3";
            label3.Size = new System.Drawing.Size(28, 13);
            label3.TabIndex = 0;
            label3.Text = "Tipo";
            // 
            // button1
            // 
            buttonRegistrar.Location = new System.Drawing.Point(106, 286);
            buttonRegistrar.Name = "button1";
            buttonRegistrar.Size = new System.Drawing.Size(75, 23);
            buttonRegistrar.TabIndex = 2;
            buttonRegistrar.Text = "Registrar";
            buttonRegistrar.UseVisualStyleBackColor = true;
            buttonRegistrar.Click += new System.EventHandler(buttonRegistrar_Click);
            //
            //label10
            //
            label10.AutoSize = true;
            label10.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label10.Location = new System.Drawing.Point(200, 10);
            label10.Name = "label10";
            //label10.Size = new System.Drawing.Size(11, 24);
            label10.TabIndex = 9;
            label10.Text = "Nueva Emergencia";

            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
            groupBox2.ResumeLayout(false);
            groupBox2.PerformLayout();

            pan.Controls.Add(groupBox1);
            pan.Controls.Add(groupBox2);
            pan.Controls.Add(buttonRegistrar);
            // pan.Controls.Add(button2);
            pan.Controls.Add(label10);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            popupUbicacion popupubi = new popupUbicacion();
            popupubi.ShowDialog();
        }

        public void llenarPanelBuscar(Panel pan)
        {
            //Grid para llenar la tabla de emergencias
            this.dvgEmergencias = new DataGridView();
            this.dvgEmergencias.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dvgEmergencias.Location = new System.Drawing.Point(30, 150);
            this.dvgEmergencias.Name = "dvgEmergencias";
            this.dvgEmergencias.Size = new System.Drawing.Size(543, 242);
            this.dvgEmergencias.TabIndex = 9;

            //Texto donde va a ir el campo de la tabla que se va a buscar
            this.txtBoxBuscar = new TextBox();
            this.txtBoxBuscar.Location = new Point(30, 110);
            this.txtBoxBuscar.Name = "txtBoxBuscar";
            this.txtBoxBuscar.Size = new Size(200, 20);
            this.txtBoxBuscar.KeyPress += new KeyPressEventHandler(txtBoxBuscar_KeyPress);

            //label1Buscar Cabecera del "txtBoxBuscar"
            this.label1Buscar = new Label();
            this.label1Buscar.AutoSize = true;
            this.label1Buscar.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1Buscar.Location = new Point(30, 90);
            this.label1Buscar.Text = "Nombre del campo: ";
            //this.label1Buscar.Size = new Size(28, 13);

            //label2Buscar Cabecera del combo "combo1Buscar"
            this.label2Buscar = new Label();
            this.label2Buscar.AutoSize = true;
            this.label2Buscar.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2Buscar.Location = new Point(30, 30);
            this.label2Buscar.Text = "Buscar por:";
            //this.label2Buscar.Size = new Size(28, 13);

            //combo1Buscar es el combo box que indica sobre qué campo de la tabla se va a realizar la busqueda
            this.combo1Buscar = new ComboBox();
            this.combo1Buscar.Location = new Point(30, 60);
            this.combo1Buscar.Size = new System.Drawing.Size(150, 21);
            this.combo1Buscar.FormattingEnabled = true;
            this.combo1Buscar.Items.Add("Codigo de Emergencia");
            //this.combo1Buscar.Items.Add("Fecha");
            this.combo1Buscar.Items.Add("Codigo de Ubicacion");
            this.combo1Buscar.SelectedItem = "Codigo de Emergencia";

            //datos auxiliares
            string sqlCad;
            sqlCad = "select idEmergencia as Codigo_Emergencia,Nombre, Prioridad, idUbicacion as Codigo_Ubicacion";
            sqlCad = sqlCad + " from Emergencia";
            SqlDataAdapter sqlAdap;

            //Aqui se va a llenar un grid con la data de la tabla "Emergencia"
            try
            {
                sqlAdap = new SqlDataAdapter(sqlCad, Conexion.connection);
                sqlAdap.Fill(this.dtData);
                this.dvgEmergencias.DataSource = this.dtData.Tables[0];
            }
            catch
            {
                MessageBox.Show("Error de conexion a al BD");
            }

            pan.Controls.Add(label2Buscar);
            pan.Controls.Add(combo1Buscar);
            pan.Controls.Add(label1Buscar);
            pan.Controls.Add(txtBoxBuscar);
            pan.Controls.Add(dvgEmergencias);
        }

        //Este metodo se encarga de hacer una busqueda a lo GOOGLE =) osea mientras se va escribiendo el texto del campo que estas buscando
        //El grid va mostrando las filas que coinciden con lo escrito hasta el momento.

        private void txtBoxBuscar_KeyPress(object sender, KeyPressEventArgs e)
        {
            string strCad;
            string campoSeleccionado = "";
            if (e.KeyChar != Convert.ToChar(Keys.Back))
                strCad = txtBoxBuscar.Text + e.KeyChar;
            else
            {
                if (txtBoxBuscar.Text.Length > 0)
                    strCad = txtBoxBuscar.Text.Substring(0, txtBoxBuscar.Text.Length - 1);
                else
                    strCad = txtBoxBuscar.Text;
            }
            DataView dv;

            if (combo1Buscar.SelectedItem == "Codigo de Emergencia")
                campoSeleccionado = "Codigo_Emergencia";
            else if (combo1Buscar.SelectedItem == "Codigo de Ubicacion")
                campoSeleccionado = "Codigo_Ubicacion";

            dv = new DataView(this.dtData.Tables[0], campoSeleccionado + " like'%" + strCad + "%'", "Codigo_Emergencia", DataViewRowState.CurrentRows);
            this.dvgEmergencias.DataSource = dv;
        }

        private void buttonRegistrar_Click(object sender, EventArgs e)
        {

            /*
        private String idEmergencia;
        private String fecha;
        private String nombre;
        private Single prioridad;
        private String idUbicacion;
        private String idTipo;
        private String descripcion;
        private String estado;


     */
            string fecha;
            string nom="";
            int priori;
            string ubica="";
            string tipo="";
            string codigotipo = "";
            string desc=""; //falta textbox
            string est = "ESTEME001"; //falta combobox y jalar de la BD los estados

            fecha = dateTimePicker1.Value.ToString("dd/MM/yyyy");
            nom = textBox1.Text;



            if (comboTipoEmerg.Text == "")
            {
                MessageBox.Show("Debe elegir necesariamente un tipo para la emergencia", "Error");
                return;
            }
            else tipo = "" + comboTipoEmerg.SelectedItem;

            if (comboPriorEmerg.Text == "")
            {
                MessageBox.Show("Debe elegir necesariamente una prioridad para la emergencia", "Error");
                return;
            }
            else priori = Int16.Parse(comboPriorEmerg.SelectedItem.ToString());

            //AQUI IRA UBICACION BIEN IMPLEMENTADO. POR AHORA QUEDARA ASI
            //ubica = textBox2.Text;
            ubica = "UBI00001";

            codigotipo = GestorEmergencia.ObtenerCodigoTipoEmergencia(tipo);

            Emergencia emergencia = new Emergencia();
            //idEmergencia	fecha	nombre	prioridad	idUbicacion	idTipo	descripcion	estado
            emergencia.Descripcion = desc;
            emergencia.Estado = est;
            emergencia.Fecha = fecha;
            emergencia.IdTipo = codigotipo;
            emergencia.IdUbicacion = ubica;
            emergencia.Nombre = nom;
            emergencia.Prioridad = priori;

            GestorEmergencia.RegistrarEmergencia(emergencia);


        }
        
    }
}
